<template>
  <div class="root-container">
    <div class="top-svg">
      <img src="@/assets/login/shape1.svg" alt="" />
    </div>
    <div class="bottom-svg">
      <img src="@/assets/login/shape2.svg" alt="" />
    </div>
    <section class="form-container">
      <section class="wrapper">
        <header>
          <div class="logo">
            <img src="@/assets/login/logo.jpg" alt="" />
          </div>
          <h1>欢迎回来</h1>
        </header>
        <section class="main-content">
          <a-form :model="model" :label-col="{ span: 4 }" @keyup.enter="handleSubmit">
            <a-form-item name="username">
              <template #label>
                <span style="font-size: 18px">用户名</span>
              </template>
              <a-input
                size="large"
                placeholder="请输入用户名"
                v-model:value="model.username"
              ></a-input>
            </a-form-item>
            <a-form-item name="password">
              <template #label>
                <span style="font-size: 18px">密码</span>
              </template>
              <a-input
                size="large"
                placeholder="请输入密码"
                type="password"
                v-model:value="model.password"
              ></a-input>
            </a-form-item>
          </a-form>
        </section>
        <footer>
          <a-button
            :loading="loading"
            class="w-full"
            style=""
            type="primary"
            size="large"
            @click="handleSubmit"
            >登录</a-button
          >
        </footer>
      </section>
    </section>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { postAction } from '@/request/request'
import { commonUrl, userUrl } from '@/api/api'
import { ElNotification } from 'element-plus'
import { useUserStore } from '@/store/user'
import { useRouter } from 'vue-router'

const model = reactive({
  username: '',
  password: ''
})
const loading = ref(false)
const router = useRouter()
const handleSubmit = () => {
  if (loading.value) return

  loading.value = true // 开始加载

  postAction(commonUrl.login, {
    ...model
  })
    .then(res => {
      if (res.code === 200) {
        ElNotification({
          title: '登录成功',
          message: '欢迎回来，' + model.username,
          type: 'success',
          duration: 3000
        })
        const data = res.data
        // 登录后保存数据
        useUserStore().updateUser(data)
        if (res.data.idType === 0) {
          router.push({
            path: '/admin'
          }) // 用户页面
        } else {
          router.push({
            path: '/user'
          }) // 用户页面
        }
      } else {
        ElNotification({
          title: '登录失败',
          message: res.message,
          type: 'error',
          duration: 3000
        })
      }
    })
    .finally(() => {
      loading.value = false // 结束加载
    })
}
</script>

<style scoped>
.top-svg {
  position: absolute;
  transform: translate(50%, -50%);
}

.bottom-svg {
  position: absolute;
  transform: translate(-100%, 25%);
}

.root-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #dbe0f9;
  overflow: hidden;
}

.form-container {
  position: relative;
  top: -10%;
}

.wrapper {
  padding: 40px;
  background-color: #fff;
  border-radius: 20px;
  width: 30rem;
  height: 30rem;
  display: flex;
  flex-direction: column;
  gap: 20px;
  z-index: 1;

  header {
    margin-bottom: 40px;

    .logo {
      width: 70px;
      height: 70px;
      border-radius: 50px;
      background-color: $brand-primary;
      display: flex;
      justify-content: center;
      align-items: center;

      span {
        font-size: 18px;
        color: #fff;
      }
    }

    h1 {
      color: $brand-primary;
      font-size: 35px;
      font-weight: 500;
      margin-bottom: 0;
      margin-top: 40px;
    }

    p {
      color: $brand-primary;
      font-size: 18px;
      font-weight: 300;
      margin: 5px 0 0 0;
    }
  }
}
</style>
